//
//  CSBuservcerSweeping.swift
//  CurSodoboAer
//
//  Created by CurSodoboAer on 2024/11/7.
//

import UIKit

/// 他人用户中心
class CSBuservcerSweeping: UIViewController {
    private let csbBackComButton = UIButton.init()
    private let csbAlertComButton = UIButton.init()
    private let headCSB = UIImageView()
    private let nameCountLblbCSB = UILabel.init()
    
    private let csbFollowComButton = UIButton.init()
    
    private let csbChatComButton = UIButton.init()
    
    
    let fansCountLblbCSB = UILabel.init()
    let likeCountLblbCSB = UILabel.init()
    
    
    var _alluserDataCSB:Dictionary<String,String>
    init(qugaoshCSB:[Int]?,alluserDataCSB: Dictionary<String, String>) {
        if qugaoshCSB?.first ?? 0 > 0 {
            self._alluserDataCSB = alluserDataCSB
        }else{
            self._alluserDataCSB = alluserDataCSB
        }
        
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var CSBColorfulDataview: UICollectionView = {
        let cofullayout = UICollectionViewFlowLayout()
        cofullayout.scrollDirection = .vertical
        cofullayout.itemSize = CGSize(width: UIScreen.main.bounds.width - 24.xQQ, height: 377)
        cofullayout.minimumLineSpacing = 10
        var colorfulDataview = UICollectionView.init(frame: .zero, collectionViewLayout: cofullayout)
    
        colorfulDataview.delegate = self
        colorfulDataview.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom:100, right: 0)
        colorfulDataview.backgroundColor = .clear
       
        colorfulDataview.dataSource = self
       
        colorfulDataview.showsVerticalScrollIndicator = false
        colorfulDataview.register(CSBUserDymCell.self, forCellWithReuseIdentifier: "CSBUserDymCellIJ")
       
        return colorfulDataview
    }()
    
  
    @objc func sarretFollowe(BUjcsb:UIButton)  {
        if BUjcsb == csbFollowComButton {
            csbFollowComButton.isSelected = !csbFollowComButton.isSelected
            if csbFollowComButton.isSelected == false  {
                for (i,ithj) in CSBFetcherAll.Hammer.folooweNewstCSB.enumerated() {
                    if ithj["csbpersonID"] == self._alluserDataCSB["csbpersonID"] {
                        CSBFetcherAll.Hammer.folooweNewstCSB.remove(at: i)
                    }
                }
            }
            
            if csbFollowComButton.isSelected == true  && CSBFetcherAll.Hammer.folooweNewstCSB.filter({ ($0["csbpersonID"]  == self._alluserDataCSB["csbpersonID"]) }).count == 0{
                CSBFetcherAll.Hammer.folooweNewstCSB.insert(_alluserDataCSB, at: 0)
                
            }
            
        }
        
        if BUjcsb == csbChatComButton {
            var disog = CSBChatPack(whoCSB: self._alluserDataCSB, chatCSB: Array<(isMine: Bool, comment: String)>())
            
            if let disoAready = CSBFetcherAll.Hammer.allMyChatListCSB.filter({ ($0.whoToMeCSBDic  == self._alluserDataCSB) }).first{
                disog = disoAready
            }else{
                CSBFetcherAll.Hammer.allMyChatListCSB.append(disog)
            }
          
            self.navigationController?.pushViewController(CSBMesgEnterSweeping.init(qugaoshCSB:[334,445],mesgDataCSB:disog ), animated: true)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if CSBFetcherAll.Hammer.folooweNewstCSB.filter({ ($0["csbpersonID"]  == self._alluserDataCSB["csbpersonID"]) }).count == 0 {
            self.csbFollowComButton.isSelected = false
        }else{
            self.csbFollowComButton.isSelected = true
        }
        NotificationCenter.default.addObserver(self, selector: #selector(csbButtonClick), name: NSNotification.Name.init("laheiCSBkUsers"), object: nil)
        
        csbFollowComButton.addTarget(self, action: #selector(sarretFollowe(BUjcsb:)), for: .touchUpInside)
        csbChatComButton.addTarget(self, action: #selector(sarretFollowe(BUjcsb:)), for: .touchUpInside)
       
        
        
        headCSB.image = UIImage(named: _alluserDataCSB["csbpersonHead"] ?? "")
        nameCountLblbCSB.text = _alluserDataCSB["csbpersonName"] ?? ""
        
        fansCountLblbCSB.text = _alluserDataCSB["csbAllFolow"] ?? ""
        likeCountLblbCSB.text = _alluserDataCSB["csbAllIlike"] ?? ""
       
        
        nameCountLblbCSB.textColor = .white
        nameCountLblbCSB.font = UIFont(name: "Futura Medium", size: 26)
        view.backgroundColor = UIColor(red: 0.64, green: 0.41, blue: 0.98, alpha: 1)
        let fanstextLblbCSB = UILabel.init()
        fanstextLblbCSB.textColor = .white
        fanstextLblbCSB.font = UIFont(name: "Helvetica", size: 12)
        fanstextLblbCSB.textColor = UIColor(white: 1, alpha: 0.6)
        fanstextLblbCSB.text = "Fans"
        fanstextLblbCSB.textAlignment = .center
        
        let liketextLblbCSB = UILabel.init()
        liketextLblbCSB.textColor = .white
        liketextLblbCSB.font = UIFont(name: "Helvetica", size: 12)
        liketextLblbCSB.textColor = UIColor(white: 1, alpha: 0.6)
        liketextLblbCSB.text = "Likes"
        liketextLblbCSB.textAlignment = .center
        
      
        
        let monmenttextLblbCSB = UILabel.init()
        monmenttextLblbCSB.textColor = .white
        monmenttextLblbCSB.font = UIFont(name: "Futura Medium", size: 22)
        monmenttextLblbCSB.textColor = UIColor(white: 1, alpha: 1)
        monmenttextLblbCSB.text = "Moments"
        
        
        let linrt = UIView.init()
        linrt.backgroundColor = UIColor.init(white: 1, alpha: 0.6)
        
        fansCountLblbCSB.textAlignment = .center
        likeCountLblbCSB.textAlignment = .center
        fansCountLblbCSB.textColor = .white
        likeCountLblbCSB.textColor = .white
        fansCountLblbCSB.font = UIFont(name: "Futura Medium", size: 18)
        likeCountLblbCSB.font = UIFont(name: "Futura Medium", size: 18)
        
        csbChatComButton.backgroundColor = UIColor(red: 0.07, green: 0.07, blue: 0.07, alpha: 1)
        csbChatComButton.setImage(UIImage.init(named: "center_mesg"), for: .normal)
        csbChatComButton.layer.cornerRadius = 25
        csbChatComButton.layer.masksToBounds = true
        csbChatComButton.setTitle("  Message", for: .normal)
        csbChatComButton.setTitleColor(.white, for: .normal)
        csbChatComButton.titleLabel?.font = UIFont(name: "Helvetica", size: 15)
        csbBackComButton.setImage(UIImage.init(named: "csb_back"), for: .normal)
        csbBackComButton.addTarget(self, action: #selector(csbButtonClick), for: .touchUpInside)
        csbAlertComButton.setImage(UIImage.init(named: "active_report"), for: .normal)
        csbAlertComButton.addTarget(self, action: #selector(csbrepotrtClick), for: .touchUpInside)
        headCSB.layer.cornerRadius = 25
        headCSB.layer.masksToBounds = true
        headCSB.contentMode = .scaleAspectFill
        csbFollowComButton.setImage(UIImage.init(named:"tocsb_foloo"), for: .normal)
        csbFollowComButton.setImage(UIImage.init(named:"aread_foloo"), for: .selected)
        view.addSubview(csbBackComButton)
        view.addSubview(csbAlertComButton)
        view.addSubview(headCSB)
        view.addSubview(csbFollowComButton)
        
        view.addSubview(fansCountLblbCSB)
        view.addSubview(fanstextLblbCSB)
        
        view.addSubview(likeCountLblbCSB)
        view.addSubview(liketextLblbCSB)
        view.addSubview(nameCountLblbCSB)
        view.addSubview(csbChatComButton)
        view.addSubview(linrt)
        view.addSubview(monmenttextLblbCSB)
        
        
        
        view.addSubview(CSBColorfulDataview)
        
        let stattusBarHeight = UIApplication.shared.statusBarFrame.height
        csbBackComButton.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xQQ)
            make.width.equalTo(24)
            make.height.equalTo(22)
            make.top.equalToSuperview().offset(stattusBarHeight + 15)
        }
        
        csbAlertComButton.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.trailing.equalToSuperview().inset(12.xQQ)
            make.centerY.equalTo(csbBackComButton)
        }
        headCSB.snp.makeConstraints { make in
            make.leading.equalToSuperview().inset(12.xQQ)
            make.width.equalTo(280.xQQ)
            make.height.equalTo(280.xQQ)
            make.top.equalTo(csbBackComButton.snp.bottom).offset(16.xQQ)
        }
        
        csbFollowComButton.snp.makeConstraints { make in
            make.width.height.equalTo(54.xQQ)
            make.top.equalTo(headCSB.snp.top).offset(-13.xQQ)
            make.right.equalTo(headCSB).offset(13.xQQ)
        }
        fansCountLblbCSB.snp.makeConstraints { make in
            make.leading.equalTo(headCSB.snp.trailing)
            make.trailing.equalToSuperview()
            make.top.equalTo(headCSB.snp.top).offset(112.xQQ)
        }
        
        fanstextLblbCSB.snp.makeConstraints { make in
            make.leading.equalTo(headCSB.snp.trailing)
            make.trailing.equalToSuperview()
            make.top.equalTo(fansCountLblbCSB.snp.bottom).offset(6.xQQ)
        }
        
        likeCountLblbCSB.snp.makeConstraints { make in
            make.leading.equalTo(headCSB.snp.trailing)
            make.trailing.equalToSuperview()
            make.top.equalTo(fanstextLblbCSB.snp.bottom).offset(54.xQQ)
        }
        
        liketextLblbCSB.snp.makeConstraints { make in
            make.leading.equalTo(headCSB.snp.trailing)
            make.trailing.equalToSuperview()
            make.top.equalTo(likeCountLblbCSB.snp.bottom).offset(6.xQQ)
        }
        
        nameCountLblbCSB.snp.makeConstraints { make in
            make.left.equalTo(headCSB.snp.left)
            make.top.equalTo(headCSB.snp.bottom).offset(26.xQQ)
        }
        
        csbChatComButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(12.xQQ)
            make.width.equalTo(176.xQQ)
            make.height.equalTo(48.xQQ)
            make.centerY.equalTo(nameCountLblbCSB)
        }
        
        linrt.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(12.xQQ)
            make.height.equalTo(1)
            make.top.equalTo(csbChatComButton.snp.bottom).offset(32.xQQ)
        }
        
        monmenttextLblbCSB.snp.makeConstraints { make in
            make.leading.equalTo(linrt)
            make.top.equalTo(linrt.snp.bottom).offset(24.xQQ)
        }
        
        CSBColorfulDataview.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.top.equalTo(monmenttextLblbCSB.snp.bottom).offset(12.xQQ)
        }
        
        
    }
    

    @objc func csbButtonClick(_ sender: UIButton) {
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

        if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
            self.navigationController?.popViewController(animated: true)
        }

        
    }
    
    @objc func csbrepotrtClick()  {
        
        let comenrAlrtPick = CSChoiUserOperaSweeping.init()
        comenrAlrtPick.userDataTranCSB = self._alluserDataCSB
        let navo =  CSBONabviSweeping.init(rootViewController:comenrAlrtPick )
        var csbBayui = [Int]()
        let csbNUmber = Array(repeating: 10, count: 64)
        let csbNumbe2 = Array(repeating: 96, count: 50)
        var cabSunmAll: Int = 0

        csbBayui.append(11)
        cabSunmAll = csbNUmber.count
        if cabSunmAll > 5 {
            csbBayui.append(contentsOf: csbNUmber)
        }else{
            csbBayui.append(contentsOf: csbNumbe2)
        }

        csbBayui.append(contentsOf: [52, 6, 11])
        csbBayui.removeLast()
        cabSunmAll = csbBayui.count

        if (cabSunmAll >= 0) && (csbBayui.count >= 0) {
            navo.modalPresentationStyle = .overCurrentContext
            self.present(navo, animated: true)
        }

        
        
    
    }
}


extension CSBuservcerSweeping:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
    
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let nui = collectionView.dequeueReusableCell(withReuseIdentifier: "CSBUserDymCellIJ", for: indexPath) as! CSBUserDymCell
        nui.backgroundColor = .clear
        nui.comekwith(uhuie: _alluserDataCSB)

        return nui
        
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      
        self.navigationController?.pushViewController(CSBDymXqSweeping.init(qugaoshCSB:[334,445],activeDataCSB: _alluserDataCSB), animated: true)
    }
    
    
}
